package com.example.springsecurity.util;

import com.example.springsecurity.security.dto.LoginUserDto;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author wuancheng
 * @date 2024/6/20 14:27
 * @description 获取存在SpringSecurity的用户工具类
 */
public class SecurityUtils {
    /**
     * 获取用户
     **/
    public static LoginUserDto getLoginUser() {
        return (LoginUserDto) getAuthentication().getPrincipal();
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }
}
